#!/usr/bin/python3

from work2 import work2

import threading
from threading import Thread
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED

count = 0
retval = 0

def work1():
    global count
    print("Thread job : {}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))))
    print('Thread Count: %d' % threading.active_count())
    return 25
    # if count > 10:
    #     return 25

def listener(event):
    global retval
    if not event.exception:
        # print("return value: ", event.retval)
        retval = event.retval

def main_work():
    global count
    global retval
    while True and count < 5:
    # while True and not retval:
        print("main work.")
        time.sleep(3)
        count += 1

if __name__ == "__main__":
    # t1 = Thread(target=work1)
    # t1.start()
    scheduler = BackgroundScheduler()
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    print("current_time: ", current_time)
    scheduler.add_listener(listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
    scheduler.add_job(work2, 'interval', seconds=8, start_date='2021-03-29 11:29:00', max_instances=2)
    scheduler.start()

    main_work()
    print("exit_time: ", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    print("return value: ", retval)

    # t1.join()